package udp_socket

import (
	"fmt"
	"github.com/panjf2000/gnet/v2"
)

type ClientHandler struct {
	*gnet.BuiltinEventEngine
}

func (client *ClientHandler) OnBoot(e gnet.Engine) (action gnet.Action) {
	_, err := e.Dup()
	if err != nil {
		fmt.Printf("[ERROR] client boot occurred error. %+v\n", err)
		action = gnet.Shutdown
	}
	return
}

func (client *ClientHandler) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
	return nil, gnet.None
}

func (client *ClientHandler) OnTraffic(c gnet.Conn) (action gnet.Action) {
	var (
		err               error
		buf               []byte
		socket            = c.Fd()
		inboundBufferSize int
	)
	action = gnet.None
	inboundBufferSize = c.InboundBuffered()
	fmt.Printf("[INFO] receive response data size %d\n", inboundBufferSize)
	buf, err = c.Next(-1)
	if err != nil {
		fmt.Printf("[ERROR] receive response data from '%d' occurred error.%+v\n", socket, err)
		return
	}
	fmt.Printf("[INFO]  receive response data from '%d' content: %s\n", socket, string(buf))
	return
}
